﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Select : System.Web.UI.Page
{
    String strCon;
    String strCom;
    SqlConnection objCon;
    SqlCommand objCom;
    SqlDataReader dtReader;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
            DeleteData(Convert.ToInt32(Request.QueryString["id"]));
        BindData();
    }

    private void BindData()
    {
        strCon = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
        objCon = new SqlConnection(strCon);
        objCon.Open();
        strCom = "Select Cus_ID,Cus_FirstName,Cus_LastName From NontananYe_SQLLogin_1.Customer";
        objCom = new SqlCommand(strCom, objCon);

        dtReader = objCom.ExecuteReader();
        GridView1.DataSource = dtReader;
        GridView1.DataBind();

        dtReader.Close();
        dtReader = null;
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        Label lblCustomerID = (Label)(e.Row.FindControl("lblCustomerID"));
        if (lblCustomerID != null)
            lblCustomerID.Text = DataBinder.Eval(e.Row.DataItem, "Cus_ID").ToString();

        Label lblCustomerFirstName = (Label)(e.Row.FindControl("lblCustomerFirstName"));
        if (lblCustomerFirstName != null)
            lblCustomerFirstName.Text = DataBinder.Eval(e.Row.DataItem, "Cus_FirstName").ToString();

        Label lblCustomerLastName = (Label)(e.Row.FindControl("lblCustomerLastName"));
        if (lblCustomerLastName != null)
            lblCustomerLastName.Text = DataBinder.Eval(e.Row.DataItem, "Cus_LastName").ToString();

        LinkButton LinkButton1 = (LinkButton)(e.Row.FindControl("LinkButton1"));
        if (LinkButton1 != null)
            LinkButton1.PostBackUrl = "Edit.aspx?id=" + DataBinder.Eval(e.Row.DataItem, "Cus_ID").ToString();

        LinkButton LinkButton2 = (LinkButton)(e.Row.FindControl("LinkButton2"));
        if (LinkButton2 != null)
            LinkButton2.PostBackUrl = "Select.aspx?id=" + DataBinder.Eval(e.Row.DataItem, "Cus_ID").ToString();
    }

    protected void DeleteData(int id)
    {
        strCon = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
        objCon = new SqlConnection(strCon);
        objCon.Open();
        strCom = "DELETE FROM NontananYe_SQLLogin_1.Customer_Detail WHERE CusDetail_Cus_ID = " + id +
            ";DELETE FROM NontananYe_SQLLogin_1.Customer WHERE Cus_ID = " + id;
        objCom = new SqlCommand(strCom, objCon);
        objCom.ExecuteNonQuery();
    }
}